package org.jeecg.client;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
/**
 * @author bright
 * 业务返回结果
 */
public class BizResponse implements Serializable{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 执行成功
	 */
	public static String OK = "OK";
	/**
	 * 执行不成功
	 */
	public static String NOK = "NOK";
    /**
     * 不能访问server
     */
    public static String ServerUnreachable = "SYS-00001";
    /**
     * 请求的server不存在
     */
    public static String ServerIsNotExist = "SYS-00002";
    /**
     * 请求的服务没有运行
     */
    public static String ServerIsNotStart = "SYS-00003";
	/**
	 * CMD不存在
	 */
	public static String CmdIsNotExist = "SYS-00004";
	/**
	 * 没有权限
	 */
	public static String NoPermission = "SYS-00005";
	/**
	 * 请求格式不正确
	 */
	public static String UnwellFormatRequest = "SYS-00006";
	static DocumentBuilder builder;
	static XPath xpath;
	private String errorCode;
	private String returnCode;
	private String errorMessage;
	private String returnMesssage;
	private Map<String, String> data;

	public BizResponse() {
		super();
	}
	public BizResponse(String errorCode) {
		super();
		this.errorCode = errorCode;
	}
	public BizResponse(String errorCode, String errorMessage) {
		super();
		this.errorCode = errorCode;
		this.errorMessage = errorMessage;
	}

	/**
	 * 将xml格式的字符串解析为response对象
	 *
	 * @param str
	 * @return
	 * @throws ParserConfigurationException
	 * @throws IOException
	 * @throws SAXException
	 * @throws XPathExpressionException
	 */
	public static BizResponse Parse(String str) {
		synchronized (BizResponse.class) {
			if (null == builder) {
				try {
					builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				} catch (ParserConfigurationException e) {
					e.printStackTrace();
				}
			}
			if (null == xpath)
				xpath = XPathFactory.newInstance().newXPath();
		}

		StringReader sr = new StringReader(str);
		BizResponse response = new BizResponse();
		try {
			InputSource is = new InputSource(sr);
			Document doc = builder.parse(is);


			response.errorCode = xpath.evaluate("/resp/ec", doc);
			response.returnCode = xpath.evaluate("/resp/rc", doc);
			response.errorMessage = xpath.evaluate("/resp/em", doc);
			response.returnMesssage = xpath.evaluate("/resp/rm", doc);

			NodeList dataNodes = (NodeList) xpath.evaluate("/resp/data/d", doc, XPathConstants.NODESET);
			for (int i = dataNodes.getLength() - 1; i > -1; i--) {
				Node n = dataNodes.item(i);
				String name = n.getAttributes().getNamedItem("n").getTextContent();
				String value = n.getTextContent();
				response.setData(name, value);
			}

			return response;
		} catch (Exception e) {
			//e.printStackTrace();
			return response;
		} finally {
			sr.close();
		}
	}

	/**
	 * 获取数据
	 * @param name
	 * @return
	 */
	public String getData(String name){
		return data == null ? null : (data.containsKey(name) ? data.get(name) : null);
	}

	/**
	 * 设置数据
	 *
	 * @param name
	 * @param value
	 */
	public void setData(String name, String value) {
		if (null == data)
			data = new HashMap<String, String>();

		data.put(name, value);
	}

	/**
	 * @return the errorCode
	 */
	public String getErrorCode() {
		return errorCode;
	}

	/**
	 * @param errorCode the errorCode to set
	 */
	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	/**
	 * @return the returnCode
	 */
	public String getReturnCode() {
		return returnCode;
	}

	/**
	 * @param returnCode the returnCode to set
	 */
	public void setReturnCode(String returnCode) {
		this.returnCode = returnCode;
	}

	/**
	 * @return the errorMessage
	 */
	public String getErrorMessage() {
		return errorMessage;
	}

	/**
	 * @param errorMessage the errorMessage to set
	 */
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	/**
	 * @return the returnMesssage
	 */
	public String getReturnMesssage() {
		return returnMesssage;
	}

	/**
	 * @param returnMesssage the returnMesssage to set
	 */
	public void setReturnMesssage(String returnMesssage) {
		this.returnMesssage = returnMesssage;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "BizResponse [errorCode=" + errorCode + ", returnCode="
				+ returnCode + ", errorMessage=" + errorMessage
				+ ", returnMesssage=" + returnMesssage + ", data=" + data + "]";
	}


	public Map<String, String> getData() {
		return data;
	}

	public void setData(Map<String, String> data) {
		this.data = data;
	}
}
